﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BinhSon.Dao;
using BinhSon.Services.Const;
using BinhSon.Services.Contracts;
using BinhSon.Services.Impl;

namespace BinhSon.Presentation
{
    public partial class ToChucThi : UserControl
    {
        private readonly IBinhSonServices _bsServices;

        public ToChucThi()
        {
            InitializeComponent();
            this._bsServices = new BinhSonServices();
            this.LoadHocKy();
            this.LoadKhoi();
            this.LoadLanThi();
            this.LoadLoaiKiemTra();
        }

        private void kiemTraButton_Click(object sender, EventArgs e)
        {
            //validate
            var selectedHocKy = (ComboBoxModel) hocKyComboBox.SelectedItem;
            var selectedKhoi = (ComboBoxModel) khoiComboBox.SelectedItem;
            var selectedMonHoc = (ComboBoxModel) monHocComboBox.SelectedItem;
            var selectedLanThi = (ComboBoxModel) lanThiComboBox.SelectedItem;
            var selectedLoaiKiemTra = (ComboBoxModel) loaiKiemTraComboBox.SelectedItem;
            var validate = this.ValidateKiemTra(selectedHocKy, selectedKhoi, selectedMonHoc, selectedLanThi, selectedLoaiKiemTra);
            if (!validate)
            {
                return;
            }
            var mhchk = new MonHocCuaHocKy
            {
                HocKyId = selectedHocKy.Key,
                KhoiId = selectedKhoi.Key,
                MonHocId = selectedMonHoc.Key,
                LanThiId = selectedLanThi.Key,
                HeSo = (int) HeSoDiem.Diem15Phut
            };
            var result = this._bsServices.ThemMonHocCuaHocKy(mhchk);
            if (result)
            {
                MessageBox.Show("Đã tạo kỳ thi thành công");
            }
        }

        private bool ValidateKiemTra(ComboBoxModel selectedHocKy, ComboBoxModel selectedKhoi,
            ComboBoxModel selectedMonHoc, ComboBoxModel selectedLanThi, ComboBoxModel selectedLoaiKiemTra)
        {
            //validate            
            if (selectedHocKy == null)
            {
                MessageBox.Show("Vui lòng chọn học kỳ");
                return false;
            }           
            if (selectedKhoi == null)
            {
                MessageBox.Show("Vui lòng chọn khối");
                return false;
            }            
            if (selectedMonHoc == null)
            {
                MessageBox.Show("Vui lòng chọn môn học");
                return false;
            }            
            if (selectedLanThi == null)
            {
                MessageBox.Show("Vui lòng chọn lần thi");
                return false;
            }
            if (selectedLoaiKiemTra == null)
            {
                MessageBox.Show("Vui lòng chọn loại kiểm tra");
                return false;
            }
            //kiem tra ton tai
            if (this._bsServices.CheckExistMonHocCuaHocKy(selectedHocKy.Key, selectedKhoi.Key, selectedMonHoc.Key, selectedLanThi.Key, selectedLoaiKiemTra.Key))
            {
                MessageBox.Show("Lần thi này đã được tạo");
                return false;
            }
            return true;
        }

        private void LoadHocKy()
        {
            var result = this._bsServices.ListHocKy().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                hocKyComboBox.Items.Add(comboBoxModel);                
            }
        }

        private void LoadKhoi()
        {
            var result = this._bsServices.ListKhoi().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                khoiComboBox.Items.Add(comboBoxModel);                
            }
        }

        private void LoadLanThi()
        {
            var result = this._bsServices.ListLanThi().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                lanThiComboBox.Items.Add(comboBoxModel);                
            }
        }

        private void LoadMonHoc(ComboBoxModel selectedKhoi, ComboBox targetComboBox)
        {
            var result = this._bsServices.ListMonHoc(selectedKhoi.Key).Select(x => new ComboBoxModel
            {
                Key = x.MonHocId,
                Value = x.TenMonHoc
            });
            targetComboBox.Items.Clear();
            targetComboBox.Text = "";
            foreach (var comboBoxModel in result)
            {
                targetComboBox.Items.Add(comboBoxModel);
            }
        }

        private void khoiComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedKhoi = (ComboBoxModel)khoiComboBox.SelectedItem;
            if (selectedKhoi == null)
            {
                monHocComboBox.Items.Clear();
                monHocComboBox.Text = "";
            }
            else
            {
                this.LoadMonHoc(selectedKhoi, monHocComboBox);
            }
        }        

        private void LoadLoaiKiemTra()
        {
            var result = this._bsServices.GetListOfHeSoDiem().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                loaiKiemTraComboBox.Items.Add(comboBoxModel);
            }
        }

        private bool Validation(ComboBoxModel selectedHocKy, ComboBoxModel selectedKhoi, ComboBoxModel selectedMonHoc, ComboBoxModel selectedLanThi, ComboBoxModel selectedLoaiThi)
        {
            //validate            
            if (selectedHocKy == null)
            {
                MessageBox.Show("Vui lòng chọn học kỳ");
                return false;
            }
            
            if (selectedKhoi == null)
            {
                MessageBox.Show("Vui lòng chọn khối");
                return false;
            }
           
            if (selectedMonHoc == null)
            {
                MessageBox.Show("Vui lòng chọn môn học");
                return false;
            }
            
            if (selectedLanThi == null)
            {
                MessageBox.Show("Vui lòng chọn lần thi");
                return false;
            }
            
            if (selectedLoaiThi == null)
            {
                MessageBox.Show("Vui lòng chọn loại thi");
                return false;
            }
            //kiem tra ton tai
            if (!this._bsServices.CheckExistMonHocCuaHocKy(selectedHocKy.Key, selectedKhoi.Key, selectedMonHoc.Key, selectedLanThi.Key, selectedLoaiThi.Key))
            {
                MessageBox.Show("Lần thi này chưa được tạo");
                return false;
            }
            return true;
        }

        private void dsPhongThiButton_Click(object sender, EventArgs e)
        {
            var selectedHocKy = (ComboBoxModel)hocKyComboBox.SelectedItem;
            var selectedKhoi = (ComboBoxModel)khoiComboBox.SelectedItem;
            var selectedMonHoc = (ComboBoxModel)monHocComboBox.SelectedItem;
            var selectedLanThi = (ComboBoxModel)lanThiComboBox.SelectedItem;
            var selectedLoaiThi = (ComboBoxModel)loaiKiemTraComboBox.SelectedItem;
            var validate = this.Validation(selectedHocKy, selectedKhoi, selectedMonHoc, selectedLanThi, selectedLoaiThi);
            if (!validate)
            {
                return;
            }
            var dialog = new SaveFileDialog
            {
                Filter = "Excel Workbook(*.xlsx)|*.xlsx|Excel 97-2003 Workbook(*.xls)|*.xls"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var dataSet = this._bsServices.ExportListOfPhongThi(selectedHocKy.Key, selectedKhoi.Key, selectedMonHoc.Key, selectedLanThi.Key, selectedLoaiThi.Key);

                    var result = CreateExcelFile.CreateExcelDocument(dataSet, dialog.FileName);
                    MessageBox.Show(result ? "File đã được tạo thành công." : "Quá trình tạo file thất bại.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not save file to disk: " + ex.Message);
                }
            } 
        }

        private void fileGhiDiemButton_Click_1(object sender, EventArgs e)
        {
            var selectedHocKy = (ComboBoxModel)hocKyComboBox.SelectedItem;
            var selectedKhoi = (ComboBoxModel)khoiComboBox.SelectedItem;
            var selectedMonHoc = (ComboBoxModel)monHocComboBox.SelectedItem;
            var selectedLanThi = (ComboBoxModel)lanThiComboBox.SelectedItem;
            var selectedLoaiThi = (ComboBoxModel)loaiKiemTraComboBox.SelectedItem;
            var validate = this.Validation(selectedHocKy, selectedKhoi, selectedMonHoc, selectedLanThi, selectedLoaiThi);
            if (!validate)
            {
                return;
            }
            var dialog = new SaveFileDialog
            {
                Filter = "Excel Workbook(*.xlsx)|*.xlsx|Excel 97-2003 Workbook(*.xls)|*.xls"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var dataSet = this._bsServices.ExportBangChamDiem(selectedHocKy.Key, selectedKhoi.Key, selectedMonHoc.Key, selectedLanThi.Key, selectedLoaiThi.Key);

                    var result = CreateExcelFile.CreateExcelDocument(dataSet, dialog.FileName);
                    MessageBox.Show(result ? "File đã được tạo thành công." : "Quá trình tạo file thất bại.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not save file to disk: " + ex.Message);
                }
            } 
        }

        private void fileMaHoaButton_Click_1(object sender, EventArgs e)
        {
            var selectedHocKy = (ComboBoxModel)hocKyComboBox.SelectedItem;
            var selectedKhoi = (ComboBoxModel)khoiComboBox.SelectedItem;
            var selectedMonHoc = (ComboBoxModel)monHocComboBox.SelectedItem;
            var selectedLanThi = (ComboBoxModel)lanThiComboBox.SelectedItem;
            var selectedLoaiThi = (ComboBoxModel)loaiKiemTraComboBox.SelectedItem;
            var validate = this.Validation(selectedHocKy, selectedKhoi, selectedMonHoc, selectedLanThi, selectedLoaiThi);
            if (!validate)
            {
                return;
            }
            var dialog = new SaveFileDialog
            {
                Filter = "Excel Workbook(*.xlsx)|*.xlsx|Excel 97-2003 Workbook(*.xls)|*.xls"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var dataSet = this._bsServices.ExportFileMaHoa(selectedHocKy.Key, selectedKhoi.Key, selectedMonHoc.Key, selectedLanThi.Key, selectedLoaiThi.Key);

                    var result = CreateExcelFile.CreateExcelDocument(dataSet, dialog.FileName);
                    MessageBox.Show(result ? "File đã được tạo thành công." : "Quá trình tạo file thất bại.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not save file to disk: " + ex.Message);
                }
            } 
        }

        private void ToChucThi_Load(object sender, EventArgs e)
        {

        }              
    }
}
